<?

JCAT_Package::ImportClass('JCAT_ViewGtk',JCAT::PACKAGE_MVCGTK) ;

class UICtrlContainerView extends JCAT_ViewGtk implements JCAT_IViewForm
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$aParent=null	JCAT_ViewGtk		拥有此视图的对象
	 * @param	$aUI=null		JCAT_UIGtkGlade		模版引擎
	 * @return	array
	 */
	public function UICtrlContainerView( JCAT_IViewContainer $aParent, JCAT_UIGtkGlade $aUI=null )
	{
		$this->JCAT_ViewGtk('UICtrlContainerView','UICtrlContainerView','UICtrlContainerView',$aParent,$aUI) ;

		$this->AddUICtrl(new TreeUICtrlChildren(),'Children') ;
	}
	
	
	/**
	 * 更新子控件清单
	 * 
	 * @access	public
	 * @return	void
	 */
	public function LoadChildrenUICtrl()
	{
		// 得到模型
		$aTreeUICtrlChildren = $this->GetUICtrl('TreeUICtrlChildren') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTreeUICtrlChildren) ;
		$aModel = $aTreeUICtrlChildren->GetGtkModel() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aModel) ;
		
		// 清除 已有的
		$aModel->clear() ;
		
		// 交由完成 加载
		$aDoc = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDoc) ;
		$aDoc->LoadChildrenUICtrl($aModel) ;
	}
	


	/**
	 * 响应 BtnDeleteOption 按钮
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnDeleteOptionClicked()
	{
		list($aModel,$aItem) = $this->GetSelectedItem(true) ;
		if(!$aItem)
		{
			return ;
		}
		
		$sItemPath = $aModel->get_string_from_iter($aItem) ;
		if( strstr($sItemPath,':')!==false )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('只能删除直属的 子控件。')) ;
			return ;
		}
		
		// 从文档中删除
		$aDoc = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDoc,'UICtrlContainerDoc') ;

		if( $aDoc->RemoveUICtrl( intval($sItemPath)) )
		{
			// 重新载入清单
			$this->LoadChildrenUICtrl() ;	
		}		
	}

	/**
	 * 响应 BtnEditIUCtrl 按钮
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnEditIUCtrlClicked()
	{
		list($aModel,$aItem) = $this->GetSelectedItem(true) ;
		if(!$aItem)
		{
			return ;
		}
	
		$sItemPath = $aModel->get_string_from_iter($aItem) ;
		if( strstr($sItemPath,':')!==false )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('只能编辑直属的 子控件。')) ;
			return ;
		}
		
		// UI控件的 相对路径
		$sUICtrlRelativePath = $aModel->get_value($aItem,TreeUICtrlChildren::CLM_PATH) ;
		
		$aParentController = $this->GetParentController() ;
		$aTopController = $aParentController->GetTopController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTopController) ;
		$sThisPath = $aTopController->GetPath() ;
		
		if( is_file($sThisPath) )
		{
			$sThisPath = dirname($sThisPath) ;
		}
		
		// UI控件的 路径
		$arrArg['path'] = JCAT_Global::TidyPath($sThisPath).$sUICtrlRelativePath ;
		
		if( is_file($arrArg['path']) )
		{
			$aOpen = new OpenCoderDocument() ;
			$aOpen->MainRun( &$arrArg ) ;
		}
	}
	
	public function GetSelectedItem($bAlert=false)
	{
		$aTreeUICtrlChildren = $this->GetUICtrl('TreeUICtrlChildren') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTreeUICtrlChildren,'JCAT_UIGtkCtrlTree') ;
		
		$wndTreeUICtrlChildren = $aTreeUICtrlChildren->GetMainWidget() ;
		JCAT_ASSERT::ASSERT_INSTANCE($wndTreeUICtrlChildren,'GtkTreeView') ;
		
		// 当前选中的 item
		$aSelection = $wndTreeUICtrlChildren->get_selection() ;
		list($aModel,$aItem) = $aSelection->get_selected() ;
		
		if(!$aItem)
		{
			if($bAlert)
			{
				JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请选择要删除的项目。')) ;
			}
			return array($aModel,null) ;
		}
		
		else 
		{
			return array($aModel,$aItem) ;
		}
	}
}

?>